package com.fwx.springcloud.controller;

import com.fwx.springcloud.entities.CommonResult;
import com.fwx.springcloud.entities.Payment;
import com.fwx.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author : fengwenxiang
 * @date : 2022/6/22 19:49
 * @description：业务内容
 */
@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;
    @Autowired
    private PaymentService paymentService;
    @PostMapping("/Payment/insert")
    public CommonResult insert(@RequestBody Payment payment){
        int result = paymentService.insertRow(payment);
        log.info("*******插入结果:"+result);
        if(result>0){
            return new CommonResult(200,"插入数据库成功,serverPort="+serverPort,result);
        }else{
            return new CommonResult(200,"插入数据库失败",null);
        }
    }

    @GetMapping("/Payment/getInfo/{idv}")
    public CommonResult getPaymentById(@PathVariable("idv") long l){
        Payment payment = paymentService.getPaymentById(l);
        log.info("*******查询结果:"+payment);
        if(payment != null){
            return new CommonResult(200,"查询成功,serverPort="+serverPort,payment);
        }else{
            return new CommonResult(200,"查询失败",payment);
        }

    }

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB() {
        return serverPort;//返回服务接口
    }
    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout()
    {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }
}
